package com.example.travelservlet.entertainmentPlan.service;

import com.example.travelservlet.entity.EntertainmentPlan;
import com.example.travelservlet.entertainmentPlan.repository.EntertainmentPlanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author 30697
 */
@Service
public class EntertainmentPlanServiceImpl implements EntertainmentPlanService {
    @Autowired
    private EntertainmentPlanRepository entertainmentPlanRepository;

    @Override
    public List<EntertainmentPlan> findEntertainmentPlansByPlanId(String planId) {
        Sort.Order sequenceOrder = Sort.Order.asc("sequence");
        Sort sort = Sort.by(sequenceOrder);
        return entertainmentPlanRepository.findEntertainmentPlansByPlanId(planId,sort);
    }

    @Override
    public EntertainmentPlan createEntertainmentPlan(EntertainmentPlan entertainmentPlan) {
        if (entertainmentPlan.getEntertainmentPlanId()==null){
            entertainmentPlan.setEntertainmentPlanId(UUID.randomUUID().toString());

        }
        return entertainmentPlanRepository.saveAndFlush(entertainmentPlan);
    }

    @Override
    public void deleteEntertainPlan(EntertainmentPlan entertainmentPlan) {
        entertainmentPlanRepository.delete(entertainmentPlan);
    }

    @Override
    public EntertainmentPlan updateEntertainmentPlanByPlanId(EntertainmentPlan entertainmentPlan) {
        return  entertainmentPlanRepository.saveAndFlush(entertainmentPlan);
    }

    @Override
    public List<EntertainmentPlan> createEntertainmentPlans(List<EntertainmentPlan> entertainmentPlans) {
        for (EntertainmentPlan entertainmentPlan:entertainmentPlans){
            entertainmentPlanRepository.save(entertainmentPlan);
        }
        return entertainmentPlans;
    }

    @Override
    public EntertainmentPlan findEntertainmentPlanByPlanIdAndSequence(int sequence,String planId) {
        return entertainmentPlanRepository.findEntertainmentPlanByPlanIdAndSequence(planId,sequence);
    }


}
